package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.ProjectInfoService;
import com.mxpio.erp.common.plm.service.ProjectTaskService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Tag(name = "ProjectInfoController}", description = "项目信息")
@RestController
@RequestMapping("/erp/plm/pro/info/")
public class ProjectInfoController {

    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private ProjectTaskService projectTaskService;

    @GetMapping("list")
    @Operation(summary = "项目列表", description = "项目列表（无分页）", method = "GET")
    public Result<List<ProjectInfo>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
//        projectInfoService.calculateProgress();//计算进度
        List<ProjectInfo> list = projectInfoService.list(ProjectInfo.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "项目列表", description = "项目列表（分页）", method = "GET")
    public Result<Page<ProjectInfo>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
//        projectInfoService.calculateProgress();//计算进度
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfo> pageResult = projectInfoService.listPage( page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("pageforblm")
    @Operation(summary = "项目列表(接口)", description = "项目列表(接口)（分页）", method = "GET")
    public Result<Page<ProjectInfo>> pageforblm(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfo> pageResult = projectInfoService.listPage( page, criteria);
        return Result.OK(pageResult);
    }

    /**
     *  查询我负责的项目列表
     * @param criteria
     * @param pageSize
     * @param pageNo
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("page/head")
    @Operation(summary = "我负责的项目列表", description = "我负责的项目列表（分页）", method = "GET")
    public Result<Page<ProjectInfo>> myHead(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfo> pageResult = projectInfoService.listPageHead(page,criteria);
        return Result.OK(pageResult);
    }

    /**
     * 查询我参与的项目列表
     * @param criteria
     * @param pageSize
     * @param pageNo
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("page/join")
    @Operation(summary = "查询我参与的项目列表", description = "查询我参与的项目列表（分页）", method = "GET")
    public Result<Page<ProjectInfo>> myJoin(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfo> pageResult = projectInfoService.listPageJoin(page,criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据项目编码获取项目列表", description = "根据项目编码获取项目列表", method = "GET")
    public Result<ProjectInfo> getById(@PathVariable(name = "proCode", required = true) String proCode) {
        ProjectInfo projectInfo = projectInfoService.getById(ProjectInfo.class, proCode);
        return Result.OK(projectInfo);
    }

    /**
     * 新增，编辑 接口
     * @param projectInfo
     * @return
     */
    @RequestMapping("save")
    @Operation(summary = "保存项目信息", description = "保存项目信息")
    public Result<ProjectInfo> add(@RequestBody ProjectInfo projectInfo) {
        projectInfo.setOverdueDay(0L);
        projectInfoService.save(projectInfo, new SmartCrudPolicyAdapter() {
            @Override
            public boolean beforeInsert(CrudContext context){
                Object o = context.getEntity();
                if (o instanceof ProjectDeliverable){
                    ProjectInfo info = context.getParent();
                    ((ProjectDeliverable) o).setProCode(info.getProCode());
                }
                return  true;
            }
        });
        projectTaskService.change(projectInfo);
        return Result.OK(projectInfo);
    }

    @RequestMapping("add/{templateCode}")
    @Operation(summary = "按模版导入信息", description = "按模版导入信息")
    public Result<?> addByTemplate(@PathVariable(name = "templateCode") String templateCode) {
        return projectInfoService.add(templateCode);
    }
//
//    @PutMapping("edit")
//    @Operation(summary = "编辑项目信息", description = "编辑项目信息", method = "PUT")
//    public Result<ProjectInfo> edit(@RequestBody ProjectInfo projectInfo) {
//        projectInfoService.updatePro(projectInfo);
//        return Result.OK(projectInfo);
//    }

    @DeleteMapping(value = "delete/{proCode}")
    @Operation(summary = "删除项目信息",description = "删除项目信息", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "proCode") String proCode) {
        projectInfoService.deleteAll(proCode);
        return Result.OK("删除成功");
    }

//    @RequestMapping("/approval")
//    @Operation(summary = "立项项目申请",description = "立项项目申请")
//    public Result<?> approval(@RequestBody ProjectInfo projectInfo) {
//        projectInfoService.approval(projectInfo);
//        return Result.OK("立项成功");
//    }

//    @RequestMapping("/approval/ok")
//    @Operation(summary = "立项项目审核",description = "立项项目审核")
//    public Result<?> approvalOK(@RequestBody ProjectInfoApproval projectInfoApproval ) {
//        projectInfoService.approvalOK(projectInfoApproval);
//        return Result.OK("立项审核成功");
//    }
    @RequestMapping("approval/ok/{key}")
    @Operation(summary = "立项项目审核",description = "立项项目审核")
    public Result<?> approvalOK(
                                @PathVariable(name = "key") String key,
                                @RequestParam(value="businessKey", required = false) String businessKey,
                                @RequestBody Map<String, Object> properties){
//        @RequestBody ProjectInfo projectInfo,
        return projectInfoService.approvalOK(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }

    @RequestMapping("approval/success")
    @Operation(summary = "立项项目结果",description = "立项项目结果")
    public Result<?> approvalSuccess(@RequestBody ProjectInfo info ) {
        return projectInfoService.approvalSuccess(info);
    }

    /**
     * 终止项目审批发起
     * @param
     * @return
     */
    @RequestMapping("end/{key}")
    @Operation(summary = "终止项目",description = "终止项目")
    public Result<?> end(@PathVariable(name = "key") String key,
                         @RequestParam(value="businessKey", required = false) String businessKey,
                         @RequestBody Map<String, Object> properties) {
        return projectInfoService.end(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }

    /**
     * 终止项目审批结束
     * @param projectInfoEnd
     * @return
     */
    @RequestMapping("end/ok")
    @Operation(summary = "终止项目",description = "终止项目")
    public Result<?> endOK(@RequestBody ProjectInfoEnd projectInfoEnd) {
        return projectInfoService.endOK(projectInfoEnd);
    }


    /**
     * 项目结项-审批发起
     * @param
     * @return
     */
    @RequestMapping("complete/{key}")
    @Operation(summary = "结项项目",description = "结项项目")
    public Result<?> complete(@PathVariable(name = "key") String key,
                              @RequestParam(value="businessKey", required = false) String businessKey,
                              @RequestBody Map<String, Object> properties) {
        return projectInfoService.complete(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }

    /**
     * 项目结项-审批结束
     * @param projectInfoComplete
     * @return
     */
    @RequestMapping("complete/ok")
    @Operation(summary = "结项项目",description = "结项项目")
    public Result<?> complete(@RequestBody ProjectInfoComplete projectInfoComplete) {
        return projectInfoService.completeOK(projectInfoComplete);
    }

    @RequestMapping("change/{key}")
    @Operation(summary = "项目变更申请",description = "项目变更申请")
    public Result<?> change(@PathVariable(name = "key") String key,
                            @RequestParam(value="businessKey", required = false) String businessKey,
                            @RequestBody Map<String, Object> properties) {
        return projectInfoService.change(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }

    @RequestMapping("change/Ok")
    @Operation(summary = "项目变更审核",description = "项目变更审核")
    public Result<?> changeOK(@RequestBody ProjectInfoChange projectInfoChange) {
        return projectInfoService.changeOK(projectInfoChange);
    }

    @GetMapping("one/{proCode}")
    @Operation(summary = "根据项目编码查询任务各个状态的数量", description = "根据项目编码获取项目列表", method = "GET")
    public Result<?> getOneNumber(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getTaskNumber(proCode);
    }

    @GetMapping("project/progress/{proCode}")
    @Operation(summary = "根据项目编码查询项目的进展", description = "根据项目编码查询项目的进展", method = "GET")
    public Result<?> getProgress(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getProgress(proCode);
    }

    @GetMapping("project/problem/{proCode}")
    @Operation(summary = "根据项目编码查询项目问题分布", description = "根据项目编码查询项目问题分布", method = "GET")
    public Result<?> getProblemDistribution(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getProblemDistribution(proCode);
    }

    @GetMapping("project/danger/{proCode}")
    @Operation(summary = "根据项目编码查询项目问题分布", description = "根据项目编码查询项目问题分布", method = "GET")
    public Result<?> getDangerDistribution(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getDangerDistribution(proCode);
    }

    @GetMapping("project/people/head/{proCode}")
    @Operation(summary = "根据项目编码查询成员负责的模块数量--任务成员", description = "根据项目编码查询成员负责的模块数量--任务成员", method = "GET")
    public Result<?> getPeople(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getPeople(proCode);
    }

    @GetMapping("project/people/position/{proCode}")
    @Operation(summary = "根据项目编码查询项目所有成员的-角色", description = "根据项目编码查询项目所有成员的-角色", method = "GET")
    public Result<?> getInfoPeople(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getPeoplePosition(proCode);
    }

    @GetMapping("project/people/time/{proCode}")
    @Operation(summary = "根据项目编码查询任务人员的工时", description = "根据项目编码查询任务人员的工时", method = "GET")
    public Result<?> getPeopleTime(@PathVariable(name = "proCode", required = true) String proCode) {
        return projectInfoService.getPeopleTime(proCode);
    }

    @GetMapping("get/child/de/{proCode}")
    @Operation(summary = "根据项目编码查询任务的交付物-分页", description = "根据项目编码查询任务的交付物-分页", method = "GET")
    public Result<?> getChildDeliverable(@PathVariable(name = "proCode", required = true) String proCode,
                                         @Parameter(description="每页条数")Integer pageSize,
                                         @Parameter(description="页号")Integer pageNo,
                                         @Parameter(description="查询参数") Criteria criteria) {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        return projectInfoService.getChildDeliverable(proCode,page,criteria);
    }
}

